package com.lanyan.web.controller.courier;

import com.lanyan.common.annotation.Log;
import com.lanyan.common.annotation.RepeatSubmit;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.BaseEntity;
import com.lanyan.common.core.domain.PageR;
import com.lanyan.common.core.domain.R;
import com.lanyan.common.enums.BusinessType;
import com.lanyan.common.utils.SecurityUtils;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.Courier;
import com.lanyan.system.domain.Evaluate;
import com.lanyan.system.domain.vo.EvaluateVo;
import com.lanyan.system.service.IAccountService;
import com.lanyan.system.service.ICourierService;
import com.lanyan.system.service.IEvaluateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@Api(tags = {"配送员-评价"})
@RequestMapping("/courier/evaluate")
public class CourierEvaluateController extends BaseController {
    @Autowired
    private IEvaluateService evaluateService;
    @Autowired
    private ICourierService courierService;
    @Autowired
    private IAccountService accountService;

    /**
     * 获取评价统计
     */
    @GetMapping("/count")
    @ApiOperation("获取评价统计")
    public R count() {
        Long courierId = SecurityUtils.getUserId();
        Courier courier = courierService.getById(courierId);
        Map<String,Object> res = new HashMap<>();
        BigDecimal all = BigDecimal.ZERO;
        if (!courier.getEvaluateTotal().equals(0L)){
            all = new BigDecimal(courier.getEvaluateSum())
                    .divide(new BigDecimal(courier.getEvaluateTotal()),2, BigDecimal.ROUND_HALF_UP);
        }
        Integer good = evaluateService.lambdaQuery()
                .eq(EvaluateVo::getDelFlag, "0")
                .eq(EvaluateVo::getCourierId, courierId)
                .eq(EvaluateVo::getDistribution, "5")
                .count();
        Integer medium = evaluateService.lambdaQuery()
                .eq(EvaluateVo::getDelFlag, "0")
                .eq(EvaluateVo::getCourierId, courierId)
                .between(EvaluateVo::getDistribution, "3","4")
                .count();
        Integer bed = evaluateService.lambdaQuery()
                .eq(EvaluateVo::getDelFlag, "0")
                .eq(EvaluateVo::getCourierId, courierId)
                .between(EvaluateVo::getDistribution, "1","2")
                .count();
        res.put("all",all);
        res.put("good",good);
        res.put("medium",medium);
        res.put("bed",bed);
        return R.ok(res);
    }

    /**
     * 查询评价列表
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "t", value = "0-好评 1-中评 2-差评 3-有图", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),
    })
    @ApiOperation("查询评价列表")
    public PageR<Evaluate> list(@ApiIgnore Evaluate evaluate) {
        startPage();
        evaluate.setCourierId(SecurityUtils.getUserId());
        List<Evaluate> list = evaluateService.selectEvaluateList(evaluate);
        for (Evaluate item : list) {
            item.setAccount(accountService.getById(item.getAccountId()));
        }
        return PageR.page(list);
    }



}
